<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chkGametype = chkGametype($gametype);
if (!in_array($chkGametype, array(1, 2)))
{
    exit("error chkGametype");
}
$chk = new CheckMode_3d($curruser['acl'], 3, $gametype);
$chk->checkShow3dPage();
$show3D3S = $chk->show3D3S;
$betname_3d = judge_type($gametype);
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
switch ($chkGametype)
{
    case 1 :
        $amountTbl = "_3d";
        break;
    case 2 :
        $amountTbl = "_3s1";
        break;
    default :
        exit("error chkGametype-2");
        break;
}
require( "class_3d/SRate_3d.php" );
switch ($show3D3S)
{
    case 0 :
        $SRate_3d = new SRate_3d($gametype);
        break;
    case 1 :
        $SRate_3d = new SRate_3s($gametype);
        break;
}
$u5id = $SRate_3d->getAcl5ID($curruser['id'], $curruser['acl']);
$u5 = get_user($u5id, $gametype);
$curruser_p = user::getuser($curruser['id'], $gametype);
if ($curruser['acl'] == "1" || $curruser['acl'] == "255")
{
    exit();
}
else if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    exit();
}
else if ($conf->get("protect_start_{$tbl}") != "1")
{
    exit();
}
else if ($u5['protmod'] != "1")
{
    exit();
}
else if ($curruser['acl'] == 2 && $curruser_p['mpercent'] == 0)
{
    exit();
}
else if ($curruser['acl'] == 5 && $curruser_p['percent_sw'] == 1)
{
    exit();
}
$u5_percent_sw = $u5['percent_sw'];
unset($u5);
require( "class_3d/Bet_3d.php" );
require( "class_3d/Bet2_3d.php" );
require( "class_3d/RateConfig_3d.php" );
require( "class_3d/MyLotto_3d.php" );
require( "class_3d/Amounts_3d.php" );
require( "class_3d/BetTotal_3d.php" );
include( "class_3d/class.lotto3d.php" );
$lotto3d = new lotto3d($gametype);
$ui = new UI_3D(true);
$tpl = new Template2();
$conf = new Config();
if ($curruser['acl'] != 255)
{
    $userlist = GetUserList($curruser['id']);
    if ($userlist == "")
    {
        $ui->showErrorPage("<p>还没有建立会员！</p>");
        exit();
    }
}
$RateConfig_3d = new RateConfig_3d($gametype);
$MyLotto_3d = new MyLotto_3d($gametype);
$Amounts_3d = new Amounts_3d($gametype);
switch ($show3D3S)
{
    case 0 :
        $Bet_3d = new Bet_3d($gametype);
        $Bet2_3d = new Bet2_3d($gametype);
        $paperOpen = "";
        break;
    case 1 :
        $Bet_3d = new Bet_3s($gametype);
        $Bet2_3d = new Bet2_3s($gametype);
        $paperOpen = $RateConfig_3d->getPaperOpen();
        break;
}
$today = date("Y-m-d");
$bettypegroup = getvar("palygroup");
$betmin = $conf->get("betmin_{$tbl}");
$betmin2face = $conf->get("betmin2face_{$tbl}");
if ($betmin2face <= 0)
{
    $betmin2face = $betmin;
}
unset($RateConfig_3d->playGroupArr02['rcmp']);
if ($bettypegroup == "" || !isset($RateConfig_3d->playGroupArr02[$bettypegroup]))
{
    $ui->showErrorPage("不正确的玩法页面");
    exit();
}
else if ($conf->get("protect_start_{$tbl}") != "1")
{
    $ui->showErrorPage("{$tblName}走飞模式已关闭");
    exit();
}
include_once( "agent/BetChk_3d.php" );
$BetChk = new BetChk_3d($today, $gametype);
if ($lotto_3d = $BetChk->chkBetDate($today))
{
    $paperOpen = $lotto_3d['open'];
    $lotto_id = $lotto_3d['id'];
    $BetTotal_3d = new BetTotal_3d($lotto_id, $gametype);
    $showBetTable = array();
    $rateArr = array();
    $amountsDataArr = array();
    $getRatesNumArr = array();
    $getRatesSNumArr = array();
    foreach ($_POST as $key => $val)
    {
        $betOKFlag = true;
        $val = intval($val);
        if (empty($val) || $val < 1)
        {
            $betOKFlag = false;
            continue;
        }
        $dataArrTemp = explode("_", substr($key, 5));
        if (count($dataArrTemp) != 3)
        {
            $betOKFlag = false;
            continue;
        }
        $dataArr = array(0 => $dataArrTemp[0], 1 => $dataArrTemp[2], 2 => $dataArrTemp[1]);
        $betSubIdx = "{$dataArr[0]}_{$dataArr[2]}";
        if (!in_array($dataArr[0], $RateConfig_3d->playGroupArr02[$bettypegroup]))
        {
            $betOKFlag = false;
            $errMsg = "({$tbl})[bnow_do_3d]No.1 管理走飞:不正确的玩法:id-" . $dataArr[0];
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            $ui->showErrorPage("走飞失败-1");
            exit();
        }
        $chkNum = $RateConfig_3d->chkG3G6Idx($dataArr[0]);
        if ($chkNum !== false)
        {
            $gDataArr = $RateConfig_3d->mystr_split($dataArr[1], 1);
            if (count($gDataArr) != $chkNum)
            {
                $betOKFlag = false;
                $showBetTable[] = array("bettype" => $betname_3d[$betSubIdx], "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "勾选个数异常，无法下注");
                continue;
            }
            sort($gDataArr);
            $dataArr[1] = implode("", $gDataArr);
        }
        else if ($bettypegroup == "r1pass")
        {
            $temVal = explode("|", $dataArr[1]);
            if (count($temVal) != 3)
            {
                write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "({$tbl})一字过关，管理走飞下注内容格式错误" . count($temVal));
                exit();
            }
            $numArr = $temVal;
            sort($numArr);
            $i = 0;
            while ($i < 3)
            {
                $numArr[$i] = intval($numArr[$i]);
                ++$i;
            }
            if (!$RateConfig_3d->chkMixIdx($dataArr[0], $numArr))
            {
                $betOKFlag = false;
                $errMsg = "({$tbl})[bnow_do_3d]No.2 管理走飞下注:不正确的内容(一字过关):id-{$dataArr[0]},num-" . implode(",", $numArr);
                write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
                $ui->showErrorPage("走飞失败");
                exit();
            }
            $dataArr[1] = $numArr;
            $dataArr1Str = implode(",", $numArr);
        }
        else if (!$RateConfig_3d->checkBetNumber($dataArr[0], $dataArr[1]))
        {
            $betOKFlag = false;
            $errMsg = "({$tbl})[bnow_do_3d]No.3 管理走飞:不正确的内容:id-" . $dataArr[0] . ",num-" . $dataArr[1];
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            $ui->showErrorPage("走飞失败");
            exit();
        }
        $memberIds = $Bet_3d->getMyMemberIdByAcl($curruser['id'], $curruser['acl']);
        $memberAmount = 0;
        if ($memberIds != "")
        {
            $memberAmount = $Bet_3d->getSumAmountAllMem($memberIds, $lotto_id, $paperOpen);
        }
        $myAmount = $Bet2_3d->getSumAmount($curruser['id'], $lotto_id, $paperOpen);
        $chkAmount = $curruser["amount{$amountTbl}"] - $memberAmount - $myAmount;
        $betmin_tmp = in_array($dataArr[0], $g_2face_3d) ? $betmin2face : $betmin;
        if ($chkAmount < $val)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $betname_3d[$betSubIdx], "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "信用额度已满");
            continue;
        }
        if ($val < $betmin_tmp)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $betname_3d[$betSubIdx], "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "走飞额不可小於最小下注额");
            continue;
        }
        if (!isset($amountsDataArr[$betSubIdx]))
        {
            $amountsData = $Amounts_3d->getAmountsDataById($dataArr[0], $curruser['id'], $dataArr[2]);
            if ($amountsData == false)
            {
                $betOKFlag = false;
                $errMsg = "({$tbl})管理走飞:无该玩法之下注限额资料:玩法id-" . $betSubIdx;
                write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
                $showBetTable[] = array("bettype" => $betname_3d[$betSubIdx], "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "无限额资料");
                continue;
            }
            $amountsDataArr[$betSubIdx] = $amountsData;
        }
        $amount1 = $amountsDataArr[$betSubIdx]['amount1'];
        $amount1 = $amount1 ? $amount1 : "0";
        if ($amount1 < $val)
        {
            $val = $amount1;
            $bigvalNote = " , <strong style=\"color:red\">但金额已调整为单注限额</strong>";
        }
        else
        {
            $bigvalNote = "";
        }
        $tempAmount = 0;
        if (isset($gDataArr))
        {
            $bettotalDataG = 1e+010;
            foreach ($gDataArr as $gVal)
            {
                $bettotalData = $BetTotal_3d->getDataList($lotto_id, $betSubIdx, $gVal, $curruser['id'], "1", "2", true);
                $bettotalDataG = min($bettotalData['amount'], $bettotalDataG);
            }
            $tempAmount = $bettotalDataG;
        }
        else
        {
            $bettotalData = $BetTotal_3d->getDataList($lotto_id, $betSubIdx, $dataArr[1], $curruser['id'], "1", "2", true);
            $tempAmount = $bettotalData['amount'];
        }
        if ($tempAmount < $val)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $betname_3d[$betSubIdx], "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "不可超过占成额");
            continue;
        }
        $diskStr = "a";
        $disk = 1;
        $tempRateBaseArr = $SRate_3d->getRates($lotto_id, $disk, $dataArr[0], $dataArr[2]);
        if ($tempRateBaseArr == false)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $betname_3d[$betSubIdx], "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "赔率值错误");
            $errMsg = "({$tbl})管理走飞:无该玩法赔率资料:id-" . $dataArr[0];
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            continue;
        }
        if ($curruser['acl'] != "5")
        {
            $user_id = $SRate_3d->getAcl5ID($curruser['id'], $curruser['acl']);
            if ($tempRateBaseArr2 = $SRate_3d->getRatesS($lotto_id, $dataArr[0], $user_id, $dataArr[2]))
            {
                $tempRateArr = array();
                foreach ($tempRateBaseArr as $key2 => $val2)
                {
                    $tempRateArr[$key2] = $tempRateBaseArr[$key2] - $tempRateBaseArr2[$key2];
                }
                $rateArr[$betSubIdx] = $tempRateArr;
            }
            else
            {
                $rateArr[$betSubIdx] = $tempRateBaseArr;
            }
            $getRatesSNumArr[$betSubIdx] = $SRate_3d->getRatesSNumArr($lotto_id, $user_id, $dataArr[0], $dataArr[2]);
        }
        else
        {
            $rateArr[$betSubIdx] = $tempRateBaseArr;
            $getRatesSNumArr[$betSubIdx] = array();
        }
        $getRatesNumArr[$betSubIdx] = $SRate_3d->getRatesNumArr($lotto_id, $disk, $dataArr[0], $dataArr[2]);
        $rate5 = 0;
        if (isset($gDataArr))
        {
            $rate = 100000;
            $rate5 = 100000;
            foreach ($gDataArr as $gVal)
            {
                $rateTemp2 = $RateConfig_3d->getRateFromRateArr($dataArr[0], $gVal, $rateArr[$betSubIdx], $getRatesNumArr[$betSubIdx], $getRatesSNumArr[$betSubIdx]);
                $rateTemp52 = $RateConfig_3d->getRateFromRateArr($dataArr[0], $gVal, $tempRateBaseArr, array(), array());
                $rate = min($rateTemp2, $rate);
                $rate5 = min($rateTemp52, $rate5);
            }
        }
        else
        {
            $rate = $RateConfig_3d->getRateFromRateArr($dataArr[0], $dataArr[1], $rateArr[$betSubIdx], $getRatesNumArr[$betSubIdx], $getRatesSNumArr[$betSubIdx]);
            $rate5 = $RateConfig_3d->getRateFromRateArr($dataArr[0], $dataArr[1], $tempRateBaseArr, array(), array());
        }
        if ($rate <= 1)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $betname_3d[$betSubIdx], "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "赔率值错误");
            $errMsg = "({$tbl})管理走飞:赔率值错误:id-" . $dataArr[0] . ",num-" . $dataArr[1];
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            continue;
        }
        if ($betOKFlag)
        {
            $fields['user_id'] = $curruser['id'];
            $fields['prev_id'] = $curruser['prev_id'];
            $fields['lotto_id'] = $lotto_id;
            $fields['createtime'] = systime();
            $fields['acl'] = $curruser['acl'];
            $fields['bettype'] = $dataArr[0];
            $fields['subid'] = $dataArr[2];
            if (is_array($dataArr[1]))
            {
                $fields['numbers'] = "'" . implode(",", $dataArr[1]) . "'";
            }
            else
            {
                $fields['numbers'] = "'" . $dataArr[1] . "'";
            }
            $fields['amount'] = $val;
            $fields['rate'] = $rate;
            $fields['rate5'] = $rate5 ? floatval($rate5 - $rate) : 0;
            $fields['line_nums'] = "'" . $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]) . "'";
            $fields['unitmoney'] = $val;
            $fields['rame'] = "0";
            $fields['state'] = "0";
            $fields['betmode'] = "0";
            $fields['ip'] = "'" . getIP() . "'";
            $fields['percent3'] = "0";
            $fields['percent4'] = "0";
            $fields['percent5'] = "0";
            $fields['percent255'] = "0";
            $disk = "1";
            switch ($curruser['acl'])
            {
                case "2" :
                    $u3 = get_user($curruser['prev_id'], $gametype);
                    $u4 = get_user($u3['prev_id'], $gametype);
                    $u5 = get_user($u4['prev_id'], $gametype);
                    $fields['uperid3'] = $u3['id'];
                    $fields['uperid4'] = $u4['id'];
                    $fields['uperid5'] = $u5['id'];
                    $fields['percent3'] = $curruser["percent0"];
                    $fields['percent4'] = $u3["percent0"];
                    if ($u5_percent_sw == 1)
                    {
                        $fields['percent5'] = $u5["mpercent"] - $fields['percent4'] - $fields['percent3'];
                        $fields['percent255'] = 100 - $u5["mpercent"];
                    }
                    else
                    {
                        $fields['percent5'] = $u4["percent0"];
                        $fields['percent255'] = 100 - $fields['percent5'] - $fields['percent4'] - $fields['percent3'];
                    }
                    $check_percent = $fields['percent255'];
                    $fields['back2'] = $Amounts_3d->getBackArr($dataArr[0], $curruser['id'], $disk, $dataArr[2]);
                    $fields['back3'] = $Amounts_3d->getBackArr($dataArr[0], $u3['id'], $disk, $dataArr[2]);
                    $fields['back4'] = $Amounts_3d->getBackArr($dataArr[0], $u4['id'], $disk, $dataArr[2]);
                    $fields['back5'] = $Amounts_3d->getBackArr($dataArr[0], $u5['id'], $disk, $dataArr[2]);
                    break;
                case "3" :
                    $u4 = get_user($curruser['prev_id'], $gametype);
                    $u5 = get_user($u4['prev_id'], $gametype);
                    $fields['uperid3'] = $curruser['id'];
                    $fields['uperid4'] = $u4['id'];
                    $fields['uperid5'] = $u5['id'];
                    $fields['percent4'] = $curruser['percent0'];
                    if ($u5_percent_sw == 1)
                    {
                        $fields['percent5'] = $u5["mpercent"] - $fields['percent4'];
                        $fields['percent255'] = 100 - $u5["mpercent"];
                    }
                    else
                    {
                        $fields['percent5'] = $u4["percent0"];
                        $fields['percent255'] = 100 - $fields['percent5'] - $fields['percent4'];
                    }
                    $check_percent = $fields['percent255'];
                    $fields['back3'] = $Amounts_3d->getBackArr($dataArr[0], $curruser['id'], $disk, $dataArr[2]);
                    $fields['back4'] = $Amounts_3d->getBackArr($dataArr[0], $u4['id'], $disk, $dataArr[2]);
                    $fields['back5'] = $Amounts_3d->getBackArr($dataArr[0], $u5['id'], $disk, $dataArr[2]);
                    break;
                case "4" :
                    $u5 = get_user($curruser['prev_id'], $gametype);
                    $fields['uperid4'] = $curruser['id'];
                    $fields['uperid5'] = $u5['id'];
                    if ($u5_percent_sw == 1)
                    {
                        $fields['percent5'] = $u5["mpercent"];
                        $fields['percent255'] = 100 - $fields['percent5'];
                    }
                    else
                    {
                        $fields['percent5'] = $curruser["percent0"];
                        $fields['percent255'] = 100 - $fields['percent5'];
                    }
                    $check_percent = $fields['percent255'];
                    $fields['back4'] = $Amounts_3d->getBackArr($dataArr[0], $curruser['id'], $disk, $dataArr[2]);
                    $fields['back5'] = $Amounts_3d->getBackArr($dataArr[0], $u5['id'], $disk, $dataArr[2]);
                    break;
                case "5" :
                    $u5 = $curruser;
                    $fields['uperid5'] = $curruser['id'];
                    $fields['percent255'] = "100";
                    $fields['back5'] = $Amounts_3d->getBackArr($dataArr[0], $curruser['id'], $disk, $dataArr[2]);
                    break;
            }
            $check_percent = $fields['percent255'];
            if ($u5['supe_account'] != "")
            {
                $rs = getdb("select id from k_user where acl=255 and account='" . $u5['supe_account'] . "'");
                $uid = $rs->f['id'];
                $rs->close();
                $fields['back6'] = $Amounts_3d->getBackArr($dataArr[0], $uid, $disk, $dataArr[2]);
            }
            else
            {
                $fields['back6'] = "0";
            }
            if ($fields['percent255'] < 0)
            {
                header("Cache-Control: no-cache, must-revalidate");
                header("Content-Type: text/plain");
                echo "{\"msg\":\"error\"}";
                exit();
            }
            $fields['r2'] = $val * $fields['percent3'] / 100;
            $fields['r3'] = $val * $fields['percent4'] / 100;
            $fields['r4'] = $val * $fields['percent5'] / 100;
            $fields['r6'] = $val * $fields['percent255'] / 100;
            $insertFlag = $Bet2_3d->dbInsert($fields);
            $insertId = "";
            if ($dataArr[0] != 46)
            {
                $oldrate = getvar("oldRate" . $dataArr[0] . "v" . $dataArr[1]);
            }
            else
            {
                $oldrate = "";
            }
            if ($insertFlag == false)
            {
                $note = "失败";
            }
            else if ($oldrate != "" && $oldrate != $rate)
            {
                $note = "赔率已异动";
                $insertId = $insertFlag;
                $insertFlag = true;
            }
            else
            {
                $note = "储存完成";
                $insertId = $insertFlag;
                $insertFlag = true;
            }
            $showBetTable[] = array("bettype" => $betname_3d[$betSubIdx], "nums" => $RateConfig_3d->getBetContent($dataArr[0], $dataArr[1]), "rate" => $rate, "betmoney" => $val, "betflag" => $insertFlag, "note" => $note . $bigvalNote);
            if ($insertId !== "")
            {
                $lotto3d->getBet2_3D($insertId);
            }
        }
    }
    $ui->header();
    echo "<table class=\"tblMy3D\" align=\"center\">";
    echo "<thead>";
    echo "<tr><td colspan=\"6\" class=\"bgClc_" . $gametype . "\">" . $tblName . " 您下注的是：</td></tr>";
    echo "<tr class=\"title0 bgImg_" . $gametype . "\"><th>下注方式</th><th>下注内容</th><th>赔率</th><th>下注金额</th><th>下注结果</th><th>说明</th></tr></thead>";
    echo "<tbody>";
    $i = 0;
    foreach ($showBetTable as $val)
    {
        echo "<tr class=\"td2\">";
        echo "<td>" . $val['bettype'] . "</td>";
        echo "<td>" . $val['nums'] . "</td>";
        echo "<td>" . $val['rate'] . "</td>";
        echo "<td align=\"right\">" . $val['betmoney'] . "</td>";
        echo "<td>";
        if ($val['betflag'])
        {
            echo "<font color=green>储存完成</font>";
        }
        else
        {
            echo "<font color=red>失败</font>";
        }
        echo "</td>";
        echo "<td>" . $val['note'] . "</td>";
        echo "</tr>";
        ++$i;
    }
    echo "</tbody>";
    if ($i < 5)
    {
        $i = 5;
    }
    else
    {
        $i = intval($i * 0.9) + 1;
    }
    echo "<tfoot><tr><td colspan=\"6\" class=\"msgBox-yellow\">" . $tblName . " 已走飞成功，系统将於 " . $i . " 秒内自动转换页面.......<img src=\"image/loading.gif\" /><br />若页面资料尚未更新，请静待数秒後，或<input type=\"button\" value=\"回主页面\" onclick=\"javascript:myBetReload();\" class=\"colorBtn01 clrBrd_" . $gametype . "\" />手动刷新页面。</td></tr></tfoot>";
    echo "</table>";
    $timebtnType = getvar("timebtnType") == "checkbox" ? "checkbox" : "text";
    echo "<script>";
    echo ( "setTimeout(\"window.top.mainFrame.location.href='op.php?op=agent&fp=bnow_3d&playtype={$bettypegroup}&gametype={$gametype}&timebtnType={$timebtnType}'\"," . $i * 1000 ) . ");";
    echo "function myBetReload(){ window.top.mainFrame.location.replace('op.php?op=agent&fp=bnow_3d&playtype={$bettypegroup}&gametype={$gametype}&timebtnType={$timebtnType}'); }";
    echo "</script>";
    $ui->footer();
}
?>